package com.example.zhang;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.Telephony;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DBManager {
    private DBHelper dbHelper;
    private String TBNAME;

    public DBManager(Context context) {
        dbHelper = new DBHelper(context);
        TBNAME = DBHelper.TB_NAME;
    }

    public void add(RateItem item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("curname",item.getCurName());
        values.put("currate",item.getCurRate());
        db.insert(TBNAME,null,values);
        db.close();
    }

    public void addAll(ArrayList<HashMap<String, String>> list){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        for (HashMap<String, String> item : list){
            ContentValues values = new ContentValues();
            values.put("curname",item.get("ItemTitle"));
            values.put("currate",item.get("ItemDetail"));
            db.insert(TBNAME,null,values);
        }
        db.close();
    }

    public void deleteAll(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }

    public void update(RateItem item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("curname",item.getCurName());
        values.put("currate",item.getCurRate());
        db.update(TBNAME, values, "ID=?", new String[]{String.valueOf(item.getID())});
        db.close();
    }

    public ArrayList<HashMap<String,String>> listAll(){
        ArrayList<HashMap<String,String>> rateList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null,null,null,null,null,null);
        if(cursor!=null){
            rateList = new ArrayList<HashMap<String,String>>();
            while(cursor.moveToNext()){
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("ItemTitle",cursor.getString(cursor.getColumnIndex("CURNAME")));
                map.put("ItemDetail",cursor.getString(cursor.getColumnIndex("CURRATE")));
                rateList.add(map);
            }
            cursor.close();
        }
        db.close();
        return rateList;

    }
    public RateItem findById(int id){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, "ID=?",new String[]{String.valueOf(id)},null,null,null);
        RateItem rateItem = null;
        if(cursor!=null && cursor.moveToFirst()){
            rateItem = new RateItem();
            rateItem .setID(cursor.getInt(cursor.getColumnIndex("ID")));
            rateItem .setCurRate(cursor.getString(cursor.getColumnIndex("CURRATE")));
            rateItem .setCurName(cursor.getString(cursor.getColumnIndex("CURNAME")));
            cursor.close();
        }
        db.close();
        return rateItem;
    }
}
